

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Session;

import controller.BusinessDelegate;
import entity.Jugador;
import entity.Partido;

public class JugadoresSVL extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BusinessDelegate bdel = BusinessDelegate.getInstancia() ;


    public JugadoresSVL() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String action= request.getParameter("action");
	
	if(action.equals("Ver Partidos"))
	obtenerPartidos(request, response);
	else
		if(action.equals("Ver Jugadores"))
			obtenerJugadores(request, response);
	
	}

	private void obtenerJugadores(HttpServletRequest request,
			HttpServletResponse response) {
		Session session = (Session) request.getSession(true);
		
		String idClub= request.getParameter("idClub");
		List<Jugador> jugadores = bdel.VerJugadores(idClub);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Buscador.jsp");
		request.setAttribute("jugadores", jugadores);
		
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void obtenerPartidos(HttpServletRequest request, HttpServletResponse response) {
		Session session = (Session) request.getSession(true);
		
		String idClub= request.getParameter("idClub");
		List<Partido> partidos = bdel.VerPartidos(idClub);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Buscador.jsp");
		request.setAttribute("partidos", partidos);
		
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
